package com.app.util.beanutil;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.log4j.Logger;

import com.app.util.common.ReflectionUtil;
import com.app.util.common.RequestUtil;






public class BeanUtilBean {

	private  final Logger log=Logger.getLogger(BeanUtilBean.class);
	private  DataConvert convert ;
	static {
	    //注册date的转换器，即允许BeanUtils.copyProperties时的源目标的类型的值允许为空
	    ConvertUtils.register(new DateConverter(null), java.util.Date.class);
	    
	  }
	public BeanUtilBean(){
		try {
			convert=DataConvert.class.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	public  void copyProperty(Object dest,Object orig){
		try {
			BeanUtils.copyProperties(dest, orig);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	public  void populate(Object bean,Map properties){
		try {
			BeanUtils.populate(bean, properties);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * function  将表单信息注入到bean
	 * dest 目标bean
	 */
	public  void copyProperty(Object dest,HttpServletRequest request,String[] ignoreArr){
		List<String> ignoreList = new ArrayList<String>();
		if(ignoreArr != null) {
			for(int i=0;i<ignoreArr.length;i++) {
				ignoreList.add(ignoreArr[i]);
			}
		}
		Map<String,String> prop = RequestUtil.getRequestMap(request);
		String property = "" ;
		String propertyValue = "" ;
		Class<?> c = dest.getClass() ;
		for(Map.Entry<String, String> entry : prop.entrySet()){
			property = entry.getKey();
			propertyValue = entry.getValue();
			if(!ignoreList.contains(property)) {
				setProperty(dest,c,property,propertyValue);
			}
			
		}
	}
	
	public  void copyPropertyInArr(Object dest,HttpServletRequest request,String... props){
		for(int i=0,length=props.length;i<length;i++){
			String pro = props[i];
			String value = request.getParameter(pro);
			Class<?> c = dest.getClass() ;
			setProperty(dest,c,pro,value);
		}
	}
	
	
	/**
	 * 对象赋值
	 * @param obj	对象
	 * @param c		
	 * @param property	fieldName
	 * @param propertyValue	value
	 */
	public void setProperty(Object obj,Class<?> c,String property,String propertyValue){
		
		ReflectionUtil.setFieldValue(obj, property, propertyValue);
		
	}
	
		
	public String getWriteMethod(String property){
		if(property==null || property.equals("")) return "";
		return "set"+property.substring(0,1).toUpperCase()+property.substring(1,property.length());
		
	}
	public String getReadMethod(String property,Class<?> propertyClass){
		if(property==null || property.equals("")) return "";
		if(propertyClass == java.lang.Boolean.class || propertyClass ==boolean.class ){
			return "is"+property.substring(0,1).toUpperCase()+property.substring(1,property.length());
		}else{
			return "set"+property.substring(0,1).toUpperCase()+property.substring(1,property.length());
		}
	}
	
	
	
	
	
		
	
}
